<template>
  <!-- 注册页组件 -->
  <div class="main">
    <div class="login-form">
      <h2 class="login-title">会员管理系统-注册</h2>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="form"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password" type="password"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" style="float:right;" @click="submitForm()">注册</el-button>
          <el-button style="float:right;margin-right:5px" @click="gotoLogin">登陆</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import usersApi from "@/userapi/usersApi"; //引入接口
// console.log(usersApi.checkName("压缩").then(res => {;}));
export default {
  data() {
    //传参发送请求 查询用户名是否存在
    var validateName = (rule, value, callback) => {
      //value 表单的内容  callback 回调，返回提示信息
      usersApi
        .checkName(value)
        .then(res => {
          if (res.data.flag) {
            callback();
            // console.log(res.data);
          } else {
            callback(new Error("该用户名已存在"));
          }
        })
        .catch(err => {
          callback(new Error("服务器繁忙"));
        });
    };
    return {
      ruleForm: {
        name: "",
        password: ""
      },
      rules: {
        //正则
        name: [
          { required: true, message: "用户名不能为空", trigger: "blur" }, //用户名不能为空
          { validator: validateName, trigger: "blur" } //验证用户名是否存在
          //   { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        password: [{ required: true, message: "密码不能为空", trigger: "blur" }]
      }
    };
  },

  components: {},

  methods: {
    //点击登录跳转登录页面
    gotoLogin() {
      this.$router.push("/login");
    },

    //注册功能 md5加密
    submitForm() {
      //this.$refs.form 查找 ref=form 的节点 validate 验证表单是否全部通过
      this.$refs.form.validate(async valid => {
        if (valid) {
          try {
            //获取密码并加密
            let psw = this.$md5(this.ruleForm.password);
            //发送请求 吧用户名和加密好的密码传入
            let p = await usersApi.reg(this.ruleForm.name, psw);
            // console.log(p.data.message);
            if (p.data.message == "注册成功") {
              //成功的弹窗
              this.$message({
                message: "注册成功",
                type: "success"
              });
              //跳转到登录页
              this.$router.push({
                path: "/login",
                query: { name: this.ruleForm.name }
              });
            } else {
              this.$message({
                message: "注册失败",
                type: "error"
              });
            }
          } catch (err) {
            this.$message({
              message: "服务器异常",
              type: "error"
            });
          }
        } else {
          this.$message({
            message: "信息未完善",
            type: "error"
          });
        }
      });
    }
  },

  created() {
    //测试
    // usersApi.checkName("压缩").then(res => {
    //   console.log(res, 123);
    // });
    // console.log(this.$md5(this.ruleForm.password));
  }
};
</script>

<style scoped>
.login-form {
  width: 350px;
  /* 上下间隙150px, 左右自动 */
  margin: 160px auto;
  /*透明背景色*/
  background-color: rgb(255, 255, 255, 0.8);
  padding: 30px;
  padding-right: 60px;
  /* 圆角 */
  border-radius: 20px;
}
.main {
  position: absolute;
  width: 100%;
  height: 100%;
  background: url("../../assets/img/login.jpg");
  overflow: hidden;
}
/* 标题 */
.login-title {
  text-align: center;
  color: #303133;
  margin-bottom: 20px;
}
</style>